package com.example.allplan.Activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.allplan.ItemTouchHelper.JiHuaBaoTouchCallback;
import com.example.allplan.JavaBean.YiGeChouQian;
import com.example.allplan.JavaBean.YiGeChouQianXinXiJavaBean;
import com.example.allplan.JavaBean.YiGeQianJavaBean;
import com.example.allplan.MyApplication;
import com.example.allplan.MyDialog.ShuRuQianDialog;
import com.example.allplan.MyView.MyLinearLayout;
import com.example.allplan.R;
import com.example.allplan.RecyclerAdapter.QianLieBiaoAdapter;
import com.example.allplan.ShuJuKuCaoZuo.YiGeChouQianSQCaoZuo;

import java.util.ArrayList;
import java.util.Collections;

public class BianJiChouQianActivity extends BaseActivity {
    RecyclerView mrecyclerview;
    Toolbar mtoolbar;
    String wen_jian_ming, biaotiming;
    YiGeChouQianSQCaoZuo mchou_qian_caozuo;
    int id_you_biao;
    QianLieBiaoAdapter madapter;
    ArrayList<YiGeQianJavaBean> mlist;
    EditText mshuruedit;
    boolean iscunzai;
    MyApplication myapp;
    MyLinearLayout mgen;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bian_ji_chou_qian_activity);
        init();
    }

    void init() {
        wen_jian_ming = getIntent().getStringExtra("wen_jian_ming");
        biaotiming = getIntent().getStringExtra("biao_ti");
        myapp = MyApplication.getInstance();
        mgen = findViewById(R.id.bian_ji_chou_qian_gen);
        mtoolbar = findViewById(R.id.bian_ji_chou_qian_toolbar);
        setSupportActionBar(mtoolbar);
        mrecyclerview = findViewById(R.id.chou_qian_tiao_mu_recyclerview);
        mshuruedit = findViewById(R.id.shu_ru_chou_qian_ming_edittext);
        if (wen_jian_ming == null && biaotiming == null) {
            id_you_biao = 0;
            mlist = new ArrayList<YiGeQianJavaBean>();
            mlist.add(new YiGeQianJavaBean(id_you_biao++, mlist.size(), 0, "一条签"));
            mlist.add(new YiGeQianJavaBean(id_you_biao++, mlist.size(), 0, "一条签"));
            iscunzai = false;
        } else {
            mshuruedit.setText(biaotiming);
            mchou_qian_caozuo = new YiGeChouQianSQCaoZuo(this, wen_jian_ming);
            id_you_biao = mchou_qian_caozuo.du_qu_xinxi().getId_zhi_zhen();
            mlist = mchou_qian_caozuo.reloadAllChouQianTiao();
            Collections.sort(mlist);
            iscunzai = true;
        }

        madapter = new QianLieBiaoAdapter(mlist, new QianLieBiaoAdapter.onItemClickListener() {
            @Override
            public void onClick(final int pos, Context mcon) {
                ShuRuQianDialog mshuru = ShuRuQianDialog.newInstance();
                mshuru.mshuru = mlist.get(pos);
                mshuru.setOnFinish(new ShuRuQianDialog.onfinish() {
                    @Override
                    public void onFinish(YiGeQianJavaBean o) {
                        mlist.set(pos, o);
                        madapter.notifyItemChanged(pos);
                    }
                });
                mshuru.show(getSupportFragmentManager(), "edit");
            }
        }, this);
        mrecyclerview.setAdapter(madapter);
        mrecyclerview.setLayoutManager(new LinearLayoutManager(this));
        JiHuaBaoTouchCallback mth = new JiHuaBaoTouchCallback(new JiHuaBaoTouchCallback.itemtouchhelpercallback() {
            @Override
            public void onitemmove(int frompos, int topos) {
                int temp = mlist.get(frompos).getPai_xu_you_biao();
                mlist.get(frompos).setPai_xu_you_biao(mlist.get(topos).getPai_xu_you_biao());
                mlist.get(topos).setPai_xu_you_biao(temp);
                Collections.swap(mlist, frompos, topos);
                madapter.notifyItemMoved(frompos, topos);
            }

            @Override
            public void onitemswipe(int direction) {

                mlist.remove(direction);
                // Log.d("kaishi",System.currentTimeMillis()+"");
                for (int i = direction; i < mlist.size(); i++) {
                    mlist.get(i).setPai_xu_you_biao(mlist.get(i).getPai_xu_you_biao() - 1);
                }
                madapter.notifyItemRemoved(direction);
                //Log.d("kaishi1",System.currentTimeMillis()+"");
                mgen.setCantclick(true);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(450);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {

                                //madapter.notifyDataSetChanged();
                                mgen.setCantclick(false);
                                //Log.d("kaishi2",System.currentTimeMillis()+"");
                            }
                        });
                    }
                }).start();
            }
        });
        ItemTouchHelper mhelper=new ItemTouchHelper(mth);
        mhelper.attachToRecyclerView(mrecyclerview);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.bian_ji_chou_qian_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.bian_ji_yi_ge_chou_qian_wan_cheng:
                if (mshuruedit.getText().toString().equals("")) {
                    Toast.makeText(this, "请输入名称", Toast.LENGTH_SHORT).show();
                } else if (mlist.size() <= 0) {
                    Toast.makeText(this, "抽签不能为空", Toast.LENGTH_SHORT).show();
                } else {
                    if (!iscunzai) {
                        savenew();
                    } else {
                        gengxin();
                    }
                    Intent min = new Intent(this, ChouQianXianShiActivity.class);
                    min.putExtra("wen_jian_ming", wen_jian_ming);
                    min.putExtra("ming_cheng", mshuruedit.getText().toString());
                    startActivityForResult(min, 101);
                }

                break;
            case R.id.tian_jia_yi_ge_qian_item:
                mlist.add(new YiGeQianJavaBean(id_you_biao++, mlist.size(), 0, "一条签"));
                madapter.notifyDataSetChanged();
                break;
            case android.R.id.home:
                finish();
                break;

        }
        return true;
    }

    private void gengxin() {
        setResult(112);
        myapp.mSuoYouChouQianSQCaoZuo.updateoneonlytwo(new YiGeChouQian(mchou_qian_caozuo.du_qu_xinxi().getSuo_shu_id(), 0, mshuruedit.getText().toString(), wen_jian_ming));
        mchou_qian_caozuo.delete_suo_you_chuang_jian();
        mchou_qian_caozuo.write_shu_zu(mlist);
        mchou_qian_caozuo.geng_xin_xin_xi_only_three(new YiGeChouQianXinXiJavaBean(id_you_biao, mchou_qian_caozuo.du_qu_xinxi().getSuo_shu_id(), mshuruedit.getText().toString(), wen_jian_ming));
    }

    private void savenew() {
        setResult(92);
        wen_jian_ming = "chou_qian" + myapp.chou_qian_xia_biao;
        myapp.mSuoYouChouQianSQCaoZuo.writeone(new YiGeChouQian(myapp.chou_qian_xia_biao, myapp.chou_qian_shu++, mshuruedit.getText().toString(), wen_jian_ming));
        mchou_qian_caozuo = new YiGeChouQianSQCaoZuo(this, "chou_qian" + myapp.chou_qian_xia_biao);
        mchou_qian_caozuo.write_shu_zu(mlist);
        mchou_qian_caozuo.xie_ru_xin_xi(new YiGeChouQianXinXiJavaBean(id_you_biao, myapp.chou_qian_xia_biao, mshuruedit.getText().toString(), wen_jian_ming));
        myapp.putSH_Int(myapp.CHOU_QIAN_XIA_BIAO_SHARE_NAME, ++myapp.chou_qian_xia_biao);
        myapp.putSH_Int(myapp.CHOU_QIAN_SHU_SHARE_NAME, myapp.chou_qian_shu);
        iscunzai = true;
    }
}
